package cn.lanqiao.bigDate.service.data.impl;

import cn.lanqiao.bigDate.entity.data.DataAuthor;
import cn.lanqiao.bigDate.mapper.data.DataAuthorMapper;
import cn.lanqiao.bigDate.service.data.DataAuthorService;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class DataAuthorServiceImpl implements DataAuthorService {

    @Resource
    private DataAuthorMapper dataAuthorMapper;

    @Override
    public IPage<DataAuthor> findAuthorByPage(Page<DataAuthor> page, String searchAuthor) {
        QueryWrapper<DataAuthor> wrapper = new QueryWrapper<>();
        wrapper.like("id",searchAuthor);
        wrapper.or();
        wrapper.like("author_name",searchAuthor);
        wrapper.or();
        wrapper.like("author_introduce",searchAuthor);
        wrapper.or();
        wrapper.like("create_time",searchAuthor);
        wrapper.or();
        wrapper.like("create_by",searchAuthor);
        wrapper.or();
        wrapper.like("update_time",searchAuthor);
        wrapper.or();
        wrapper.like("update_by",searchAuthor);
        Page<DataAuthor> page1 = dataAuthorMapper.selectPage(page, wrapper);
        return page1;
    }

    @Override
    public Boolean addAuthor(DataAuthor dataAuthor) {
        dataAuthor.setCreateTime(LocalDateTime.now());
        dataAuthor.setUpdateTime(LocalDateTime.now());
        int insert = dataAuthorMapper.insert(dataAuthor);
        if (insert==1){
            return true;
        }
        return false;
    }

    @Override
    public List<DataAuthor> findAll() {
        QueryWrapper<DataAuthor> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        List<DataAuthor> dataAuthors = dataAuthorMapper.selectList(wrapper);
        return dataAuthors;
    }

    @Override
    public DataAuthor findAuthorOne(Long id) {
        QueryWrapper<DataAuthor> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        DataAuthor dataAuthor = dataAuthorMapper.selectOne(wrapper);
        return dataAuthor;
    }


    @Override
    public Boolean delAuthor(String ids) {
        if (!StringUtils.isEmpty(ids)){
            String[] id = ids.split(",");
            List<String> list = Arrays.asList(id);
            int i = dataAuthorMapper.deleteBatchIds(list);
            if (i>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean updataAuthor(DataAuthor dataAuthor) {
        if(dataAuthor!=null && dataAuthor.getId()!=null){
            Long id = dataAuthor.getId();
            DataAuthor authorOne = findAuthorOne(id);
            authorOne.setId(dataAuthor.getId());
            authorOne.setAuthorName(dataAuthor.getAuthorName());
            authorOne.setAuthorIntroduce(dataAuthor.getAuthorIntroduce());
            authorOne.setCreateBy(dataAuthor.getCreateBy());
            authorOne.setUpdateBy(dataAuthor.getUpdateBy());
            authorOne.setCreateTime(LocalDateTime.now());
            authorOne.setUpdateTime(LocalDateTime.now());
            int update = dataAuthorMapper.updateById(authorOne);
            if (update==1){
                return true;
            }
        }
        return false;
    }
}
